<?php

function GetProducts(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Products');
}

function GetProducts_Sorted(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Products ORDER BY id DESC');
}

function GetProduct($id){
	$conn = GetConnection();
	if($id == null)
		die("No Product could be found. I.D. invalid.");
	$results = $conn->query("SELECT * FROM Products WHERE id='$id'");
	$row = $results->fetch_assoc();
	$conn->close();
	return $row;
}

function EditProduct($row,$image,$thumbnail){
	$conn = GetConnection();
		$id = $row['id'];
		$company = $row['companies_id'];
		$product_category = $row['product_category_id'];
		$sku = $row['sku'];
		$name = strip_tags($row['name']);
		$price = $row['price'];
		$weight = $row['weight'];
		$description = $row['description'];
	if((strlen($name) > 0))
	{
		if($image == NULL){
			if($thumbnail == NULL){
			$conn->query("UPDATE Products SET Companies_id = '$company',product_category_id = '$product_category',
			sku ='$sku', name='$name',price='$price',weight='$weight',description ='$description',updatdate ='CURRENT_TIMESTAMP' WHERE id='$id'");
			}
			else {
				$conn->query("UPDATE Products SET Companies_id = '$company',product_category_id = '$product_category',
			sku ='$sku', name='$name',price='$price',weight='$weight',description ='$description',updatdate ='CURRENT_TIMESTAMP', thumbnail='$thumbnail' WHERE id='$id'");
			}
		}
		elseif($thumbnail == NULL){
			$conn->query("UPDATE Products SET Companies_id = '$company',product_category_id = '$product_category',
			sku ='$sku', name='$name',price='$price',weight='$weight',description ='$description',updatdate ='CURRENT_TIMESTAMP', image='$image' WHERE id='$id'");
		}
		else{
			$conn->query("UPDATE Products SET Companies_id = '$company',product_category_id = '$product_category',
			sku ='$sku', name='$name',price='$price',weight='$weight',description ='$description',updatdate ='CURRENT_TIMESTAMP', image='$image',thumbnail='$thumbnail' WHERE id='$id'");
		}
	}
	else
		header("Location: http://cs.newpaltz.edu/~N02432643/%20web-course-2012/final/Products/edit.php?id=".$id."");
	
	$conn->close();
}
function NewProduct($row,$image,$thumbnail){
		$company = $row['companies_id'];
		$product_category = $row['product_category_id'];
		$name = $row['name'];
		$price = $row['price'];
		$weight = $row['weight'];
		$description = $row['description'];
		$sku = rand_string(20);
		
	if(strlen($name) > 0)
	{
		$conn = GetConnection();
		//INSERT INTO Products(id,Companies_id,product_category_id,sku,name,price,weight,decription,thumbnail,image,updatdate) VALUES ('','$company','$product_category','$sku','$name','$price','$weight','$description','$thumbnail','$image','CURRENT_TIMESTAMP')
		if($conn->query("INSERT INTO Products(id,Companies_id,product_category_id,sku,name,price,weight,description,thumbnail,image,updatdate) VALUES ('','$company','$product_category','$sku','$name','$price','$weight','$description','$thumbnail','$image','CURRENT_TIMESTAMP')")){
			$conn->close();
		}
		else{
			die("Query failed");
		}
	}
	else {
			die("You need to enter a Product name.");
	}
}

function DeleteProduct($id){
		$conn = GetConnection();
		$results = $conn->query("Delete FROM Products WHERE id=$id");
		header('Location: http://cs.newpaltz.edu/~N02432643/%20web-course-2012/final/Products/');
}




function GetProductByCat($catID){
	$conn = GetConnection();
	return $conn->query("SELECT * FROM Products WHERE product_category_id='$catID'");
}

function rand_string( $length ) {
	$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";	
	$size = strlen( $chars );
	$str = '';
	for( $i = 0; $i < $length; $i++ ) {
		$str .= $chars[ rand( 0, $size - 1 ) ];
	}

	return $str;
}